package fr.dlachiche.gtasksync.network;

import java.io.IOException;

import com.google.api.services.tasks.model.TaskList;
import com.google.api.services.tasks.model.TaskLists;
import fr.dlachiche.gtasksync.auth.TasksFactory;

/**
 * Manage Task Lists between GTask and application
 * 
 * @author dlachiche
 *
 */
public class TaskListNetwork {
	
	public static void retreiveTasksLists() throws IOException {
		TaskLists taskLists = TasksFactory.getService().tasklists().list().execute();
		
		for (TaskList taskList : taskLists.getItems()) {
			System.out.println(taskList.getTitle());
		}
	}
	
	public static void retreiveTaskList() throws IOException {
		TaskList taskList = TasksFactory.getService().tasklists().get("taskListID").execute();
		
		System.out.println(taskList.getTitle());
	}
	
	public static void createTaskList() throws IOException {
		TaskList taskList = new TaskList();
		taskList.setTitle("New task list");
		
		TaskList result = TasksFactory.getService().tasklists().insert(taskList).execute();
		System.out.println(result.getId());
	}
	
	public static void updateTaskList() throws IOException {
		// First retreive the task list to update
		TaskList taskList = TasksFactory.getService().tasklists().get("taskListID").execute();
		taskList.setTitle("New task list name");
		
		TaskList result = TasksFactory.getService().tasklists().update(taskList.getId(), taskList).execute();
		System.out.println(result.getTitle());
	}
	
	public static void deleteTaskList() throws IOException {
		TasksFactory.getService().tasklists().delete("taskListID").execute();
	}

	public static void clearCompletedTasks() throws IOException {
		TasksFactory.getService().tasks().clear("taskListID").execute();
	}
}
